using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

   public class CLSREPDEV : ObservableObject
    {

       MDOgeneral mod = new MDOgeneral();
       
        public int guardar(List<CLSREPDEV> lrpev,List<CLSCONORD> lcons) {
           try {

               MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();

               foreach (CLSREPDEV repdev in lrpev) {
                   if (repdev.existe())
                   {
                        repdev.actCanDev();
                   }
                   else {
                       repdev.insertar();
                   }
               }
               foreach (CLSCONORD cosu in lcons) {
                   if (cosu.existe())
                   {
                       cosu.actCanDev();
                   }
                   else
                   {
                       cosu.insertar();
                   }               
               }              
               MDOgeneral.transaccion.Commit();
               return 1;
           }
           catch (Exception ex) {
                MDOgeneral.transaccion.Rollback();
               throw ex;
           }
       }
       
       public Boolean existe() {
           try {

               string query = "select count(*) from repdev where anosem_rdv=" + ANOSEM_RDV + " and nrosem_rdv=" + NROSEM_RDV + " and codprc_rdv='" + CODPRC_RDV + "' and codemp_rdv='" + CODEMP_RDV + "' and ordnro_rdv='" + ORDNRO_RDV + "' and codmtp_rdv='" + CODMTP_RDV + "'";

               DataTable val = mod.consulta(query);

               if (val.Rows.Count > 0) {
                   if (Convert.ToInt32(val.Rows[0][0])>0){
                       return true;
                   }
               }

               return false;


           }
           catch (Exception ex) {
               throw ex;
           }
       
       }

       public int actCanDev() {
           try {
               string query = "update REPDEV set CANDEV_RDV=" + CANDEV_RDV + " where ANOSEM_RDV=" + ANOSEM_RDV + " and NROSEM_RDV=" + NROSEM_RDV + " and CODPRC_RDV='" + CODPRC_RDV + "' and CODEMP_RDV='" + CODEMP_RDV + "' and ORDNRO_RDV='" + ORDNRO_RDV + "'";
               mod.comando(query);
               return 1;
           }
           catch (Exception ex) { throw ex; }
       }
   
       public int insertar() {
           try {
               string query = "insert into repdev (ANOSEM_RDV,NROSEM_RDV,CODPRC_RDV,CODEMP_RDV,ORDNRO_RDV,CODMTP_RDV,CANENT_RDV,CANDEV_RDV,PORCEN_RDV) values ("+ANOSEM_RDV+","+NROSEM_RDV+",'"+CODPRC_RDV+"','"+CODEMP_RDV+"','"+ORDNRO_RDV+"','"+CODMTP_RDV+"',"+CANENT_RDV+","+CANDEV_RDV+","+PORCEN_RDV+")";
               return mod.comando(query);
           }
           catch (Exception ex) { throw ex; }
       }

       private  int _ANOSEM_RDV;
       public int ANOSEM_RDV{
           get{return _ANOSEM_RDV;}
           set{ _ANOSEM_RDV=value;
           RaisePropertyChangedEvent("ANOSEM_RDV"); }
       }
 
       private  int _NROSEM_RDV;
       public int NROSEM_RDV{
           get{return _NROSEM_RDV;}
           set{ _NROSEM_RDV=value;
           RaisePropertyChangedEvent("NROSEM_RDV"); }
       }

       private  string _CODPRC_RDV;
       public string CODPRC_RDV{
           get{return _CODPRC_RDV;}
           set{ _CODPRC_RDV=value;
           RaisePropertyChangedEvent("CODPRC_RDV"); }
       }
       
       private  string _CODEMP_RDV;
       public string CODEMP_RDV{
           get{return _CODEMP_RDV;}
           set{ _CODEMP_RDV=value;
           RaisePropertyChangedEvent("CODEMP_RDV"); }
       }

       private  string _ORDNRO_RDV;
       public string ORDNRO_RDV{
           get{return _ORDNRO_RDV;}
           set{ _ORDNRO_RDV=value;
           RaisePropertyChangedEvent("ORDNRO_RDV"); }
       }

       private  string _CODMTP_RDV;
       public string CODMTP_RDV{
           get{return _CODMTP_RDV;}
           set{ _CODMTP_RDV=value;
           RaisePropertyChangedEvent("CODMTP_RDV"); }
       }
       
       private  double _CANENT_RDV;
       public double CANENT_RDV{
           get{return _CANENT_RDV;}
           set{ _CANENT_RDV=value;
           RaisePropertyChangedEvent("CANENT_RDV"); }
       }
              
       private  double _CANDEV_RDV;
       public double CANDEV_RDV{
           get{return _CANDEV_RDV;}
           set{ _CANDEV_RDV=value;
           RaisePropertyChangedEvent("CANDEV_RDV"); }
       }

       private  double _PORCEN_RDV;
       public double PORCEN_RDV{
           get{return _PORCEN_RDV;}
           set{ _PORCEN_RDV=value;
           RaisePropertyChangedEvent("PORCEN_RDV"); }
       }



       public CLSMAEMAT maeMat = new CLSMAEMAT();


       public CLSCONORD conOrd = new CLSCONORD();
   }
}
